/**
 * 
 */
package com.globallogic.training.university;

import java.util.*;
import java.text.*;

import com.globallogic.training.university.people.*;
import com.globallogic.training.university.study.*;
import com.globallogic.training.university.property.*;

/**
 * A <code>University</code> object represents a university
 * 
 * @author vasiliy.kulgaviy
 * 
 */
public class University {
	private Schedule generalSchedule = new Schedule();
	private List<Student> students = new ArrayList<Student>();
	private List<Teacher> teachers = new ArrayList<Teacher>();
	private List<Course> courses = new ArrayList<Course>();
	// private Auditorium[] auditoriums = new Auditorium[0];
	private List<Auditorium> auditoriums = new ArrayList<Auditorium>();

	private static final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat(
			"yyyy-MM-dd");;

	/**
	 * Prints all students of University to console
	 */
	public void printStudents() {
		System.out.println(Strings.TITLE_REPORT_STUDENTS);

		for (Student student : students) {
			System.out.printf(Strings.ROW_STUDENT, student.toString(),
					DATE_FORMATTER.format(student.getBirthDate()),
					student.getGrade());
		}
	}

	/**
	 * Prints all teachers of University to console
	 */
	public void printTeachers() {
		System.out.println(Strings.TITLE_REPORT_TEACHERS);

		for (Teacher teacher : teachers) {
			System.out.printf(Strings.ROW_TEACHER, teacher.toString(),
					DATE_FORMATTER.format(teacher.getBirthDate()));
		}
	}

	/**
	 * Makes a general schedule of the University
	 */
	public void makeSchedule() {
		generalSchedule.scheduleCourses(courses, auditoriums, teachers);

		generalSchedule.assignStudents(students);
	}

	/**
	 * Returns Main Schedule of the University
	 * @return the generalSchedule
	 */
	public Schedule getGeneralSchedule() {
		return generalSchedule;
	}

	/**
	 * Returns Teachers of the University
	 * @return the teachers
	 */
	public List<Teacher> getTeachers() {
		return teachers;
	}

	/**
	 * Returns Students of the University
	 * @return the students
	 */
	public List<Student> getStudents() {
		return students;
	}

	/**
	 * Adds to University predefined data (students, courses etc)
	 * 
	 * @throws ParseException
	 */
	public void initialize() throws ParseException {
		addStudent(new Student("Ivan", "Petrov", parseDate("1995-12-01")), 1);
		addStudent(
				new Student("Nikolay", "Vernigora", parseDate("1995-03-23")), 2);

		addTeacher(new Teacher("Oleh", "Kolov", parseDate("1965-05-20")));
		addTeacher(new Teacher("Tetiana", "Onegina", parseDate("1973-02-3")));

		addCourse(new Course("AAA-1", "Computer Science Intruduction", 1));
		courses.get(0).addLesson(new Lecture("What is HelloWorld?"));
		courses.get(0).addLesson(new Lecture("How to implement HelloWorld"));
		courses.get(0).addLesson(new Seminar("Developing HelloWorld"));

		addCourse(new Course("MATH-1", "Mathematics Basics", 1));
		courses.get(1).addLesson(new Lecture("Integral"));
		courses.get(1).addLesson(new Lecture("Differential"));
		courses.get(1).addLesson(new Seminar("What is the difference?"));

		addCourse(new Course("AAA-2", "Java Basics", 2));
		courses.get(2).addLesson(new Lecture("Java Basics"));
		courses.get(2).addLesson(new Seminar("Java Basics Practicum"));
		courses.get(2).addLesson(new Lecture("OOD"));
		courses.get(2).addLesson(new Seminar("OOD Practicum"));

		teachers.get(1).assignCourse(courses.get(0));
		teachers.get(0).assignCourse(courses.get(1));
		teachers.get(1).assignCourse(courses.get(2));

		addAuditorium(new Auditorium("1"));
		addAuditorium(new Auditorium("2"));
		addAuditorium(new Auditorium("3"));
	}

	private void addStudent(Student student, int grade) {
		students.add(student);
		student.setGrade(grade);
	}

	private void addTeacher(Teacher teacher) {
		teachers.add(teacher);
	}

	private void addCourse(Course course) {
		courses.add(course);
	}

	private void addAuditorium(Auditorium auditorium) {
		auditoriums.add(auditorium);
	}

	private static Date parseDate(String inputString) throws ParseException {
		return DATE_FORMATTER.parse(inputString);
	}

	/**
	 * Start point of the application
	 * 
	 * @param args don't use any argument
	 */
	public static void main(String[] args) throws ParseException {
		University university = new University();

		university.initialize();

		university.makeSchedule();

		university.printStudents();
		System.out.println("");
		university.printTeachers();

		Schedule schedule = university.getGeneralSchedule();
		List<Teacher> teachers = university.getTeachers();
		List<Student> students = university.getStudents();

		System.out.println("");
		schedule.printSchedule();

		System.out.println("");
		schedule.printSchedule(teachers.get(1));

		System.out.println("");
		schedule.printSchedule(students.get(0));
	}
}
